﻿using Sustainalytics.DynamicsCRMGateway;
using Sustainalytics.DynamicsCRMGateway.Entities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CRMDataExporter
{
    class Program
    {
        static void Main(string[] args)
        {
            var connString = ConfigurationManager.ConnectionStrings["SustainalyticsCRM"].ConnectionString;
            string path = ConfigurationManager.AppSettings["DumpPath"];

            IDynamicsCrmGateway crmGateway = DynamicsCrmGateway.GetInstance(connString);
            
            //SerializeAccounts(crmGateway.ListAccounts(), Path.Combine(path, "accounts.csv"));
            SerializeFeatureClaims(crmGateway.ListFeatureClaims(), Path.Combine(path, "featureClaims.csv"));
            SerializeAccountFeatures(crmGateway.ListAccountFeatures(), Path.Combine(path, "accountFeatures.csv"));
            SerializeFeatures(crmGateway.ListFeatures(), Path.Combine(path, "features.csv"));
            SerializeClaims(crmGateway.ListClaims(), Path.Combine(path, "claims.csv"));
        }

        private const string sep = "\t\t\t";
        private static readonly string lineSep = Environment.NewLine;

        private static void SerializeClaims(IEnumerable<Claim> claims, string filePath)
        {
            var sb = new StringBuilder();
            claims.ToList().ForEach(a =>
            {
                sb.Append(a.Id.ToString());
                sb.Append(sep);
                if (a.Name.Contains(sep))
                {
                    sb.Append("\"");
                    sb.Append(a.Name);
                    sb.Append("\"");
                }
                else
                    sb.Append(a.Name);
    
                sb.Append(sep);
                sb.Append(a.Value);
                sb.Append(lineSep);
            });

            File.WriteAllText(filePath, sb.ToString());
        }

        private static void SerializeFeatureClaims(IEnumerable<FeatureClaimBridge> featureClaims, string filePath)
        {
            var sb = new StringBuilder();
            featureClaims.ToList().ForEach(a =>
            {
                sb.Append(a.FeatureId == null ? "" : a.FeatureId.ToString());
                sb.Append(sep);
                sb.Append(a.ClaimId == null ? "" : a.ClaimId.ToString());
                sb.Append(lineSep);
            });

            File.WriteAllText(filePath, sb.ToString());
        }

        private static void SerializeFeatures(IEnumerable<Feature> features, string filePath)
        {
            var sb = new StringBuilder();

            features.ToList().ForEach(a =>
            {
                sb.Append(a.Id.ToString());
                sb.Append(sep);
                if (a.Name.Contains(sep))
                {
                    sb.Append("\"");
                    sb.Append(a.Name);
                    sb.Append("\"");
                }
                else
                    sb.Append(a.Name);

                sb.Append(sep);
                sb.Append(a.ParentFeature == null ? "" : a.ParentFeature.Id.ToString());
                sb.Append(lineSep);
            });

            File.WriteAllText(filePath, sb.ToString());
        }

        private static void SerializeAccountFeatures(IEnumerable<AccountFeature> accountFeatures, string filePath)
        {
            var sb = new StringBuilder();
            accountFeatures.ToList().ForEach(a =>
            {
                sb.Append(a.Account == null ? "" : a.Account.Id.ToString());
                sb.Append(sep);
                sb.Append(a.Feature == null ? "" : a.Feature.Id.ToString());
                sb.Append(lineSep);
            });

            File.WriteAllText(filePath, sb.ToString());
        }

        /*private static void SerializeAccounts(IEnumerable<CrmAccount2> accounts, string filePath)
        {
            var sb = new StringBuilder();

            accounts.ToList().ForEach(a=>{
                sb.Append(a.Id.ToString());
                sb.Append(sep);
                if (a.Name.Contains(sep))
                {
                    sb.Append("\"");
                    sb.Append(a.Name);
                    sb.Append("\"");
                }
                else
                    sb.Append(a.Name);

                sb.Append(sep);
                sb.Append((int)a.Status);
                sb.Append(sep);
                sb.Append(a.HasPlatformAccess?1:0);
                sb.Append(lineSep);
            });

            File.WriteAllText(filePath, sb.ToString());
        }*/
    }
}
